package Message;

import Business.FinalAll;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志存储类
 */
public class Storage {

    private static double timeSave = 60;//日志相隔保存时间(分钟)
    private static boolean runing = true;//程序是否在运行
    private static Thread threadClock;//时钟线程

    /**
     * 程序关闭
     */
    public static void stop(){
        runing = false;//设置关闭
        try {
            if (threadClock != null) threadClock.interrupt();
        }catch (Exception e){
            e.printStackTrace();
            Tips.Warm(e.getMessage());
        }
        save();//保存文件
    }

    /**
     * 时钟启动
     */
    public static boolean ClockStart(){
        Date startDate = new Date();
        threadClock = new Thread(new Runnable() {
            @Override
            public void run() {
                while (runing){//循环
                    try {
                        Thread.sleep((int)(timeSave * 60 * 1000));//休眠
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Tips.Warm("时钟休眠异常！");
                    }
                    if (runing) {
                        Tips.Info("日志正在保存！");
                        Date dateStart = new Date();
                        save();//保存文件
                        Date dateEnd = new Date();
                        long span = dateEnd.getTime() - dateStart.getTime();
                        Tips.Info("日志保存完毕！耗时：" + span + "毫秒");
                    }
                }
            }
        });
        threadClock.start();
        Date endDate = new Date();
        long span = endDate.getTime() - startDate.getTime();
        Tips.Info("日志时钟启动正常！耗时： " + span + " 毫秒");
        return true;
    }

    /**
     * 日志数据存储变量
     */
    private static StringBuilder data = new StringBuilder();

    /**
     * 添加日志数据
     * @param text 需要添加的信息
     */
    public static void addData(String text){
        synchronized (data) {//资源锁
            data.append(text);
        }
    }

    /**
     * 保存日志信息到本地
     */
    private static void save(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Date date = new Date();
        File file = new File(FinalAll.projectName + "/log/" + dateFormat.format(date) + ".log");
        //创建文件
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            synchronized (data) {//锁住资源输出
                bufferedWriter.write(data.toString());
                data.delete(0 , data.length());//清空内容
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bufferedWriter != null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }




}
